package ct.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import ct.data.JeuxObligations;
import ct.data.Parametres;
import ct.main.Lancement;
import ct.model.Flux;
import ct.model.Obligation;
import ct.model.Portefeuille;
import ct.model.TableModelPortefeuille;

public class PanelPortefeuille extends JPanel {

	private static final long serialVersionUID = 1L;

	private TablePortefeuille table;
	private FenetrePortefeuille parent;

	public PanelPortefeuille(FenetrePortefeuille parent) {
		super(new BorderLayout());
		this.parent = parent;
		table = new TablePortefeuille();
		this.add(table, BorderLayout.CENTER);
		this.add(new BoutonsPortefeuille(this), BorderLayout.SOUTH);
	}

	public Portefeuille getPortefeuille() {
		return table.getPortefeuille();
	}

	public void MAJ() {
		table.miseAJour();
		parent.revalidate();
	}

	public TablePortefeuille getTable() {
		return table;
	}
}

class TablePortefeuille extends JPanel {

	private static final long serialVersionUID = 1L;

	private TableModelPortefeuille model;
	private JTable table;

	public TablePortefeuille() {
		super(new BorderLayout());
		model = new TableModelPortefeuille();
		table = new JTable(model);
		this.add(new JScrollPane(table));
	}

	protected void miseAJour() {
		this.removeAll();
		table = new JTable(model);
		this.add(new JScrollPane(table));
	}

	protected Portefeuille getPortefeuille() {
		return model.getPortefeuille();
	}

	protected int getSelection() {
		return table.convertRowIndexToModel(table.getSelectedRow());
	}

}

class BoutonsPortefeuille extends JPanel {

	private static final long serialVersionUID = 1L;
	private PanelPortefeuille parent;

	public BoutonsPortefeuille(PanelPortefeuille portef) {
		super(new GridLayout(2, 1));
		this.parent = portef;

		JPanel ligneune = new JPanel(new FlowLayout());
		JPanel lignedeux = new JPanel(new FlowLayout());

		String[] liste = new String[] { "Importer...", "OAT", "Secteur public",
				"Test" };
		JComboBox importliste = new JComboBox(liste);
		importliste.setSelectedIndex(0);
		importliste.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Portefeuille p = parent.getPortefeuille();

				p.clear();

				JComboBox cb = (JComboBox) e.getSource();
				String imp = (String) cb.getSelectedItem();
				if (imp.equals("OAT"))
					JeuxObligations.importerOAT(p);
				else if (imp.equals("Secteur public"))
					JeuxObligations.importerSecteurPublic(p);
				else if (imp.equals("Test"))
					JeuxObligations.importerTest(p);

				parent.MAJ();
			}

		});

		JButton Bajout = new JButton("Ajouter obligation");
		Bajout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new FenetreAjoutObligation(parent).setVisible(true);
			}
		});

		JButton Bsuppr = new JButton("Supprimer obligation");
		Bsuppr.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (parent.getTable().getSelection() >= 0) {
					parent.getPortefeuille().remove(
							parent.getTable().getSelection());
					parent.getPortefeuille().setMaxInterpol();
					parent.MAJ();
				}
			}
		});

		JButton Braz = new JButton("Effacer tout");
		Braz.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (JOptionPane.showConfirmDialog(null,
						"Voulez-vous tout effacer ?", "Attention",
						JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
					parent.getPortefeuille().clear();
					parent.MAJ();
				}
			}
		});

		JButton Btaux = new JButton("Courbe des taux");
		Btaux.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new FenetreCalculCourbeTaux(parent.getPortefeuille())
						.setVisible(true);
			}
		});

		JButton Becheancier = new JButton("Echeancier");
		Becheancier.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (parent.getTable().getSelection() >= 0) {
					Obligation ob = parent.getPortefeuille().get(
							parent.getTable().getSelection());

					DefaultCategoryDataset dataset = new DefaultCategoryDataset();
					Flux[] ech = ob.echeancier(Parametres.date_estimation);
					for (int i = 0; i < ech.length; i++)
						dataset.addValue(ech[i].getMontant(), Integer
								.valueOf(0), ech[i].getDate().toString());

					JInternalFrame chartframe = new JInternalFrame(
							"Echeancier", true, true, true, true);
					JFreeChart chart = ChartFactory.createBarChart(
							"Echeancier de " + ob.getLibelle(), "Date",
							"Coupon", dataset, PlotOrientation.VERTICAL, false,
							true, false);

					chartframe.setContentPane(new ChartPanel(chart));

					Lancement.mainframe.getDesktop().addFrame(chartframe);
				}
			}
		});

		JButton Bimportfile = new JButton("Importer Fichier");
		Bimportfile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser importfilechooser = new JFileChooser();
				if (importfilechooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {

					try {
						parent.getPortefeuille().clear();
						
						parent.getPortefeuille().importCSVFile(
								importfilechooser.getSelectedFile()
										.getAbsolutePath());

					} catch (IOException ioe) {
						JOptionPane.showMessageDialog(parent,
								"Impossible de lire le fichier", "Erreur IO",
								JOptionPane.ERROR_MESSAGE);
					} catch (ParseException pe) {
						JOptionPane.showMessageDialog(parent,
								"Format de fichier inconnu", "Erreur Format",
								JOptionPane.ERROR_MESSAGE);
					}

					parent.MAJ();
				}
			}
		});

		JButton Bimporturl = new JButton("Importer URL");
		Bimporturl.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new FenetreImportObligation(parent).setVisible(true);
			}
		});

		ligneune.add(importliste);
		ligneune.add(Bajout);
		ligneune.add(Bsuppr);
		ligneune.add(Braz);
		ligneune.add(Bimportfile);
		ligneune.add(Bimporturl);
		lignedeux.add(Becheancier);
		lignedeux.add(Btaux);


		this.add(ligneune);
		this.add(lignedeux);
	}

}